import { PrismaClient } from '@prisma/client'
import { registerDecorator, ValidationOptions, ValidationArguments } from 'class-validator'

export function IsExist(dataBase: string, validationOptions?: ValidationOptions) {
   return function (object: Object, propertyName: string) {
      registerDecorator({
         name: 'IsExist',
         target: object.constructor,
         propertyName: propertyName,
         options: validationOptions,
         constraints: [dataBase],
         validator: {
            async validate(value: any) {
               const prisma = new PrismaClient()
               const res = await prisma[dataBase].findFirst({
                  where: {
                     username: value
                  }
               })

               return res?.username
            }
         }
      })
   }
}
